package ejb.facades;

import entities.User;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.Clock;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Stateless
public class UserFacade extends AbstractFacade<User> implements UserFacadeLocal {
    @PersistenceContext(unitName = "Trabalho_PDPU")
    private EntityManager em;
    
    @EJB
    private Clock clock;
    

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }
    
    @Override
    public List<User> getLastOnlineUsers() {
        Calendar c = Calendar.getInstance();
        c.setTime(clock.getTime());
        c.add(Calendar.MINUTE, -10);
        return em.createNamedQuery("User.getLastOnlineUsers", User.class)
                .setParameter("minusServerDate", c.getTime())
                .setParameter("serverDate", clock.getTime())
                .getResultList();
    }

}
